{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Cloud Masking"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Make all imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ee\n",
    "ee.Initialize()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from geetools import ui, cloud_mask"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ipygee import *"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Landsat 8 SR"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d8d417c968bc4837841db7a2e489c96f",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Map(basemap={'attribution': 'Map data (c) <a href=\"https://openstreetmap.org\">OpenStreetMap</a> contributors',…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "c1bf174d9cdb43f9bbd55819080f07c6",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Tab(children=(CustomInspector(children=(SelectMultiple(options=OrderedDict(), value=()), Accordion(selected_in…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "Map = Map()\n",
    "Map.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "visL8 = {'bands':['B5','B6','B4'],'min':0, 'max':5000}\n",
    "        \n",
    "image = ee.Image('LANDSAT/LC08/C01/T1_SR/LC08_232090_20140915')\n",
    "Map.addLayer(image, visL8, 'Landsat 8 SR Original')\n",
    "Map.centerObject(image, zoom=12)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Using BQA band"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "mask_l8SR_all = cloud_mask.landsatSR()\n",
    "mask_l8SR_cloud = cloud_mask.landsatSR(['cloud'])\n",
    "mask_l8SR_shadow = cloud_mask.landsatSR(['shadow'])\n",
    "mask_l8SR_snow = cloud_mask.landsatSR(['snow'])\n",
    "\n",
    "l8sr_masked_all = mask_l8SR_all(image)\n",
    "l8sr_masked_cloud = mask_l8SR_cloud(image)\n",
    "l8sr_masked_shadow = mask_l8SR_shadow(image)\n",
    "l8sr_masked_snow = mask_l8SR_snow(image)\n",
    "\n",
    "Map.addLayer(l8sr_masked_all, visL8, 'L8SR masked all')\n",
    "Map.addLayer(l8sr_masked_cloud, visL8, 'L8SR masked cloud')\n",
    "Map.addLayer(l8sr_masked_shadow, visL8, 'L8SR masked shadow')\n",
    "Map.addLayer(l8sr_masked_snow, visL8, 'L8SR masked snow')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Landsat TOA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "5aebdfbe597c4cbe843c550f343cefdc",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Map(basemap={'attribution': 'Map data (c) <a href=\"https://openstreetmap.org\">OpenStreetMap</a> contributors',…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d066b75c27614dfabffefc3b0ca0a274",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Tab(children=(CustomInspector(children=(SelectMultiple(options=OrderedDict(), value=()), Accordion(selected_in…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "MapTOA = ui.Map(tabs=('Inspector',))\n",
    "MapTOA.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "visTOA = {'bands': visL8['bands'], 'min':0, 'max':0.5}\n",
    "i_toa = ee.Image('LANDSAT/LC08/C01/T1_TOA/LC08_232090_20140915')\n",
    "MapTOA.addLayer(i_toa, visTOA, 'Landsat 8 TOA Original')\n",
    "MapTOA.centerObject(i_toa, zoom=12)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Using BQA band"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "mask_l8TOA_all = cloud_mask.landsat8ToaBQA()\n",
    "mask_l8TOA_cloud = cloud_mask.landsat8ToaBQA(['cloud'])\n",
    "mask_l8TOA_shadow = cloud_mask.landsat8ToaBQA(['shadow'])\n",
    "mask_l8TOA_snow = cloud_mask.landsat8ToaBQA(['snow'])\n",
    "\n",
    "l8toa_masked_all = mask_l8TOA_all(i_toa)\n",
    "l8toa_masked_cloud = mask_l8TOA_cloud(i_toa)\n",
    "l8toa_masked_shadow = mask_l8TOA_shadow(i_toa)\n",
    "l8toa_masked_snow = mask_l8TOA_snow(i_toa)\n",
    "\n",
    "MapTOA.addLayer(l8toa_masked_all, visTOA, 'L8TOA masked all')\n",
    "MapTOA.addLayer(l8toa_masked_cloud, visTOA, 'L8TOA masked cloud')\n",
    "MapTOA.addLayer(l8toa_masked_shadow, visTOA, 'L8TOA masked shadow')\n",
    "MapTOA.addLayer(l8toa_masked_snow, visTOA, 'L8TOA masked snow')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Sentinel 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "236728bb3263435ab6ec060b8ab18d7f",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Map(basemap={'attribution': 'Map data (c) <a href=\"https://openstreetmap.org\">OpenStreetMap</a> contributors',…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "e253b89fa18a4050b6afd441726dd6f0",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Tab(children=(CustomInspector(children=(SelectMultiple(options=OrderedDict(), value=()), Accordion(selected_in…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "MapS2 = ui.Map(tabs=('Inspector',))\n",
    "MapS2.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "visS2 = {'bands':['B8','B11','B4'],'min':0, 'max':5000}\n",
    "is2 = ee.Image('COPERNICUS/S2/20151123T142942_20170221T180430_T18GYT')\n",
    "MapS2.centerObject(is2, zoom=12)\n",
    "MapS2.addLayer(is2, visS2, 'Sentinel 2 Original')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ESA Cloud Masking"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "ESA_mask_all = cloud_mask.sentinel2()\n",
    "is2_ESA = ESA_mask_all(is2)\n",
    "MapS2.addLayer(is2_ESA, visS2, 'Sentinel 2 ESA maked')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Hollstein Decision Tree"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "is2_Holl_all = cloud_mask.applyHollstein(is2)\n",
    "is2_Holl_cloud = cloud_mask.applyHollstein(is2, ['cloud'])\n",
    "is2_Holl_shadow = cloud_mask.applyHollstein(is2, ['shadow'])\n",
    "is2_Holl_snow = cloud_mask.applyHollstein(is2, ['snow'])\n",
    "is2_Holl_water = cloud_mask.applyHollstein(is2, ['water'])\n",
    "is2_Holl_cirrus = cloud_mask.applyHollstein(is2, ['cirrus'])\n",
    "\n",
    "MapS2.addLayer(is2_Holl_all, visS2, 'Sentinel 2 Hollstein all')\n",
    "MapS2.addLayer(is2_Holl_cloud, visS2, 'Sentinel 2 Hollstein cloud')\n",
    "MapS2.addLayer(is2_Holl_shadow, visS2, 'Sentinel 2 Hollstein shadow')\n",
    "MapS2.addLayer(is2_Holl_snow, visS2, 'Sentinel 2 Hollstein snow')\n",
    "MapS2.addLayer(is2_Holl_water, visS2, 'Sentinel 2 Hollstein water')\n",
    "MapS2.addLayer(is2_Holl_cirrus, visS2, 'Sentinel 2 Hollstein cirrus')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# MODIS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "modis = ee.ImageCollection('MODIS/006/MOD09GA')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "MapMOD = ui.Map(tabs=('Inspector',))\n",
    "MapMOD.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "site = ee.Geometry.Point([-71.8, -43])\n",
    "date = ee.Date('2017-08-01')\n",
    "visMOD = {'bands':['sur_refl_b02', 'sur_refl_b06', 'sur_refl_b01'],'min':0, 'max':5000}\n",
    "modis = modis.filterDate(date, date.advance(4, 'month'))\n",
    "i_mod = ee.Image(modis.first())\n",
    "MapMOD.addLayer(i_mod, visMOD, 'MODIS TERRA Original Image')\n",
    "MapMOD.centerObject(site, zoom=8)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Using `state_1km` band"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mod_mask = cloud_mask.modis09ga()\n",
    "i_masked = mod_mask(i_mod)\n",
    "MapMOD.addLayer(i_masked, visMOD, 'Masked MODIS')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
